package com.example.demoone.kotlin

fun main() {
    var p4 = Person4("张三丰", 100)
    p4.intro()
    var p5 = Person5("张三", 18, 100)
    p5.intro()
    var p6 = Person5("张三", 18)
    p6.intro()
    var p7 = Person5("张三")
    p7.intro()
}


class Person4(val name: String) {
    var age: Int = 0

    // 次构造函数委托给主构造
    constructor(name: String, age: Int) : this(name) {
        this.age = age // 额外初始化
    }

    fun intro() {
        println("姓名: $name, 年龄: $age")
    }
}

/**
 * 多个次构造函数
 */
class Person5(val name: String) {
    var age: Int = 0
    var score: Int = 0

    // 次构造函数1
    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }

    // 次构造函数2
    constructor(name: String, age: Int, score: Int) : this(name, age) {
        this.score = score
    }

    fun intro() {
        println("我的名字叫：$name ,我的年龄是：$age, 成绩是: $score ")
    }
}